package com.stack.auto_parts.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.stack.auto_parts.entity.Orders;
import com.stack.auto_parts.entity.SupplierOrder;
import com.stack.auto_parts.mapper.SupplierOrderMapper;
import com.stack.auto_parts.service.ProductService;
import com.stack.auto_parts.service.SupplierOrderService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Title: SupplierOrderServiceImpl
 * @Author WUYU_MIN
 * @Package com.stack.auto_parts.service.impl
 * @Date 2024/7/9 15:42
 * @description:
 */
@Service
public class SupplierOrderServiceImpl implements SupplierOrderService {
    @Autowired
    SupplierOrderMapper supplierOrderMapper;

    @Resource
    ProductService productService;


    @Override
    public void insertSupplierOrder(SupplierOrder supplierOrder) {
        System.out.println(supplierOrder);
        Integer integer = supplierOrderMapper.insert(supplierOrder);
        System.out.println(integer);
    }

    @Override
    public String seterror(String uuid) {
        QueryWrapper<SupplierOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uuid",uuid);
        SupplierOrder supplierOrder1 = supplierOrderMapper.selectOne(queryWrapper);
        if(supplierOrder1 == null){
            return "该订单不存在";
        }
        else {
            supplierOrder1.setState(1);
            Integer integer = supplierOrderMapper.updateById(supplierOrder1);
            if(integer != 1)
                return "订单异常";
        }
        return null;
    }

    @Override
    public String setInfo(SupplierOrder supplierOrder) {
        QueryWrapper<SupplierOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uuid",supplierOrder.getUuid());
        SupplierOrder supplierOrder1 = supplierOrderMapper.selectOne(queryWrapper);
        System.out.println(supplierOrder1);
        if(supplierOrder1 == null || supplierOrder1.getState() == 1) {
            return "该订单不存在或状态异常";
        }
        else {

            supplierOrder1.setAddress(supplierOrder.getAddress());
            supplierOrder1.setRemark(supplierOrder.getRemark());
            Integer numProduct = productService.getNumber(supplierOrder1.getPartName(),supplierOrder1.getSupplier());
            if(supplierOrder1.getNumber() > numProduct) {
                return "库存不够";
            }
            productService.updataNumber(supplierOrder1.getPartName(),supplierOrder1.getSupplier(),numProduct - supplierOrder1.getNumber());
            supplierOrder1.setCommodity_status(1);
            Integer integer = supplierOrderMapper.insert(supplierOrder1);

            if(integer != 1)
                return "发货失败";
        }
        return null;
    }

    @Override
    public List<SupplierOrder> list() {
        return supplierOrderMapper.selectList(null);
    }

    @Override
    public List<SupplierOrder> selectByCondition(String sipplier, Integer state, Integer commodityStatus) {
        List<SupplierOrder> supplierOrders = supplierOrderMapper.selectByCondition(sipplier, state, commodityStatus);
        return supplierOrders;
    }
}
